/**
 * gislin 2017年7月18日
 */
package com.btcode.orm.relation;

import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModel;

/**
 * @author gislin 2017年7月18日
 */
public class OneToMany implements IRelation {

    IField masterField;

    IField slaveField;

    boolean isRelationDelete = false;

    boolean isNeedCache = false;

    /**
     * 嵌套关联关系，用于处理多对多关系
     */
    IRelation nextRelation = null;

    public OneToMany(IField masterField) {
        init(masterField, null, false, false);
    }

    public OneToMany(IField masterField, IField slaveField) {
        init(masterField, slaveField, false, false);
    }

    public OneToMany(IField masterField, IField slaveField, boolean isRelationDelete) {
        init(masterField, slaveField, isRelationDelete, false);
    }

    public OneToMany(IField masterField, IField slaveField, boolean isRelationDelete,
        boolean isNeedCache) {
        init(masterField, slaveField, isRelationDelete, isNeedCache);
    }

    private void init(IField masterField, IField slaveField, boolean isRelationDelete,
        boolean isNeedCache) {
        this.masterField = masterField;
        this.slaveField = slaveField;
        this.isRelationDelete = isRelationDelete;
        this.isNeedCache = isNeedCache;
    }

    public OneToMany join(IField slaveField) {
        this.slaveField = slaveField;
        return this;
    }

    @Override
    public String getJoinText() {

        StringBuffer strBuffer = new StringBuffer();
        strBuffer.append(" LEFT JOIN ");
        strBuffer.append(slaveField.getTableName());
        strBuffer.append(" ON ");
        strBuffer.append(masterField.getTableName());
        strBuffer.append(".");
        strBuffer.append(masterField.getFieldName());
        strBuffer.append(" = ");
        strBuffer.append(slaveField.getTableName());
        strBuffer.append(".");
        strBuffer.append(slaveField.getFieldName());
        strBuffer.append(" ");
        return null;
    }

    public void setRelationDelete(boolean isRelationDelete) {
        this.isRelationDelete = isRelationDelete;
    }

    public void setNeedCache(boolean isNeedCache) {
        this.isNeedCache = isNeedCache;
    }

    public void setSlaveField(IField slaveField) {
        this.slaveField = slaveField;
    }

    @Override
    public RelationType getRelationType() {
        return RelationType.oneToMany;
    }

    @Override
    public String getMasterTableName() {
        return masterField.getTableName();
    }

    @Override
    public String getSlaveTableName() {
        return slaveField.getTableName();
    }

    @Override
    public IField getMasterField() {
        return masterField;
    }

    @Override
    public IField getSlaveField() {
        return slaveField;
    }

    @Override
    public Class<IModel> getMasterModelClass() {
        return masterField.getModelClass();
    }

    @Override
    public Class<IModel> getSlaveModelClass() {
        return slaveField.getModelClass();
    }

    @Override
    public boolean isRelationDelete() {
        return isRelationDelete;
    }

    @Override
    public IRelation getNextRelation() {
        return nextRelation;
    }

    @Override
    public void setNextRelation(IRelation relation) {
        nextRelation = relation;
    }

    @Override
    public boolean isNeedCache() {
        return isNeedCache;
    }

}
